package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Salary;
import service.Service;

/**
 * Servlet implementation class EditDetailSalarySubmitServlet
 */
@WebServlet("/EditDetailSalarySubmitServlet")
public class EditDetailSalarySubmitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditDetailSalarySubmitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int empId = Integer.parseInt(request.getParameter("empId"));
		double positionSal = Double.parseDouble(request.getParameter("positionSal"));
		double zhiwuSal = Double.parseDouble(request.getParameter("zhiwuSal"));
		double xinjiSal = Double.parseDouble(request.getParameter("xinjiSal"));
		double overtimeSal = Double.parseDouble(request.getParameter("overtimeSal"));
		double deductionSal = Double.parseDouble(request.getParameter("deductionSal"));
		double bonusSal = Double.parseDouble(request.getParameter("bonusSal"));
		int leaveDay = Integer.parseInt(request.getParameter("leaveDay"));
		int lateDay = Integer.parseInt(request.getParameter("lateDay"));
		int absentDay = Integer.parseInt(request.getParameter("absentDay"));
		double privateDuty = Double.parseDouble(request.getParameter("privateDuty"));
		double baseSal = positionSal + zhiwuSal + xinjiSal;
		double jixiaoSal = overtimeSal + deductionSal + bonusSal;
		
		double leaveSal = 50 *leaveDay;
		double lateSal = 50 *lateDay;
		double absentSal = (baseSal / 30) *absentDay;
		double punishSal = leaveSal + lateSal + absentSal;
		double allSal = positionSal + zhiwuSal + xinjiSal + overtimeSal + deductionSal + bonusSal
				+ leaveSal + lateSal + absentSal;
		double factSal = allSal - privateDuty;
		Salary salary = new Salary();
		salary.setEmpId(empId);
		salary.setPositionSal(positionSal);
		salary.setZhiwuSal(zhiwuSal);
		salary.setXinjiSal(xinjiSal);
		salary.setJixiaoSal(jixiaoSal);
		salary.setOvertimeSal(overtimeSal);
		salary.setDeductionSal(deductionSal);
		salary.setBonusSal(bonusSal);
		salary.setPunishSal(punishSal);
		salary.setLeaveSal(leaveSal);
		salary.setLateSal(lateSal);
		salary.setAbsentSal(absentSal);
		salary.setAllSal(allSal);
		salary.setPrivateDuty(privateDuty);
		salary.setFactSal(factSal);
		Service s = new Service();
		if(s.updateDetailSalary(salary)) {
			request.setAttribute("salary", salary);
			request.getRequestDispatcher("a_other/success.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("a_other/editDetaiSalary.jsp").forward(request, response);
		}
	}

}
